package com.yiyuzi.admin.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyuzi.admin.common.constant.ResponseCodeEnum;
import com.yiyuzi.admin.common.domain.PageResultDTO;
import com.yiyuzi.admin.common.domain.ResponseDTO;
import com.yiyuzi.admin.common.utils.ConvertUtils;
import com.yiyuzi.admin.privilege.domain.entity.PrivilegeEntity;
import com.yiyuzi.admin.privilege.service.PrivilegeService;
import com.yiyuzi.admin.role.RolePrivilegeDao;
import com.yiyuzi.admin.role.domain.dto.RolePrivilegeAddDTO;
import com.yiyuzi.admin.role.domain.dto.RolePrivilegeDTO;
import com.yiyuzi.admin.role.domain.dto.RolePrivilegeQueryDTO;
import com.yiyuzi.admin.role.domain.entity.RoleEntity;
import com.yiyuzi.admin.role.domain.entity.RolePrivilegeEntity;
import com.yiyuzi.admin.role.service.RolePrivilegeService;
import com.yiyuzi.admin.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yiyuzi
 * @date 2021/5/9 12:30
 */

@Service
@Transactional
public class RolePrivilegeServiceImpl extends ServiceImpl<RolePrivilegeDao, RolePrivilegeEntity> implements RolePrivilegeService {
    @Resource
    private RolePrivilegeDao rolePrivilegeDao;

    @Resource
    private RoleService roleService;

    @Resource
    private PrivilegeService privilegeService;

    /**
     * 组装对外展示的DTO
     *
     * @param rolePrivilegeDTO
     * @return
     */
    private RolePrivilegeDTO getOtherInfo(RolePrivilegeDTO rolePrivilegeDTO){
        if (rolePrivilegeDTO == null)
            return null;
        RoleEntity roleEntity = roleService.getById(rolePrivilegeDTO.getRoleId());
        PrivilegeEntity privilegeEntity = privilegeService.getById(rolePrivilegeDTO.getPrivilegeId());
        rolePrivilegeDTO.setRoleName(roleEntity.getRoleName());
        rolePrivilegeDTO.setPath(privilegeEntity.getPath());
        rolePrivilegeDTO.setPrivilegeName(privilegeEntity.getPrivilegeName());

        return rolePrivilegeDTO;
    }

    @Override
    public RolePrivilegeDTO findById(Long id) {
        return this.getOtherInfo(ConvertUtils.copy(rolePrivilegeDao.selectById(id), RolePrivilegeDTO.class));
    }

    @Override
    public RolePrivilegeDTO findByRoleIdAndPriId(Long roleId, Long priId) {
        return rolePrivilegeDao.selectByRoleIdAndPriId(roleId, priId);
    }

    @Override
    public PageResultDTO<RolePrivilegeDTO> findPage(RolePrivilegeQueryDTO queryDTO) {
        List<RolePrivilegeEntity> list = this.list(new QueryWrapper<RolePrivilegeEntity>()
                .eq(queryDTO.getId() != null,"id", queryDTO.getId())
                .eq(queryDTO.getPrivilegeId() != null, "privilege_id", queryDTO.getPrivilegeId())
                .eq(queryDTO.getRoleId() != null, "role_id", queryDTO.getRoleId()));
        List<RolePrivilegeDTO> list1 = ConvertUtils.copyList(list, RolePrivilegeDTO.class);
        for(int i = 0; i < list1.size(); i++){
            list1.set(i, this.getOtherInfo(list1.get(i)));
        }
        return ConvertUtils.convertPageResult(list1, RolePrivilegeDTO.class);
    }

    @Override
    public boolean add(RolePrivilegeAddDTO addDTO) {
        if(addDTO.getRoleId() == null || addDTO.getPrivilegeId() == null)
            return false;
        if(this.findByRoleIdAndPriId(addDTO.getRoleId(), addDTO.getPrivilegeId()) != null)
            return false;
        if(roleService.getById(addDTO.getRoleId()) == null)
            return false;
        return this.save(ConvertUtils.copy(addDTO, RolePrivilegeEntity.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(List<RolePrivilegeAddDTO> list) {
        for(RolePrivilegeAddDTO addDTO : list){
            if(! this.add(addDTO)){
                throw new RuntimeException(ResponseCodeEnum.FAIL.getDesc());
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteList(List<Long> list) {
        for(Long id: list) {
            if(!this.removeById(id))
                throw new RuntimeException(ResponseCodeEnum.FAIL.getDesc());
        }
        return true;
    }

}
